package org.come.Jpanel;

import com.tool.ModerateTask.TaskRoleData;
import com.tool.btn.ActivityBtn;
import com.tool.btn.AutoTaskBtn;
import com.tool.tcpimg.UIUtils;
import com.updateNew.MyIsif;
import org.come.bean.AutoActiveBase;
import org.come.bean.AutoActiveBaseBean;
import org.come.model.ActiveBase;
import org.come.until.CutButtonImage;
import org.come.until.UserMessUntil;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.util.List;



public class AutoTaskModelJpanel extends JPanel {


    private JTextField numField;
    private AutoActiveBase activeBase;
    //private JLabel labName;
    private JCheckBox checkBox;

    public AutoTaskModelJpanel() {
        setPreferredSize(new Dimension(465, 61));
        setOpaque(false);
        setLayout(null);

        getNumField();
        //getLabName();
        getJCheckBox();

    }

    public void showActiveBase(AutoActiveBase activeBase, int sumReceive) {
        if (activeBase == null) {
            return;
        }
        this.activeBase = activeBase;
        //labName.setText(activeBase.getaName() + "（已完成" + sumReceive + "次）");
        numField.setText(String.valueOf(activeBase.getGoodNum()));
    }

    public int getStringOneChar(String value, String oldChar) {
        String replace = value.replace(oldChar, "");
        return value.length() - replace.length();
    }

    private ImageIcon icon;

    public AutoActiveBase getCaab(){
        AutoActiveBaseBean autoActiveBaseBean = UserMessUntil.getAllAutoActive();
        List<AutoActiveBase> allActives = autoActiveBaseBean.getAllautobase();

        if (allActives != null && allActives.size()>0) {
            for (int i = 0; i < allActives.size(); i++) {
                if(this.activeBase.getId()== allActives.get(i).getId()){
                    return allActives.get(i);
                }
            }
        }
        return null;
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        AutoActiveBase autoActiveBase = this.getCaab();
        int sumReceive = 0;
        int num1 = TaskRoleData.SumReceive(activeBase.getTasksetId(), 2);
        int num2 = autoActiveBase.getComNum();
        int num3 = activeBase.getComNum();
        sumReceive = num1 > num2 ? num1 : num2;
        activeBase.setComNum(sumReceive > num3 ? sumReceive : num3);
        activeBase.setComNum(sumReceive);
        //labName.setText(activeBase.getaName() + "（已完成" + activeBase.getComNum() + "次）");
        AutoActiveBaseBean autoActiveBaseBean = UserMessUntil.getAllAutoActive();
        List<AutoActiveBase> allActives = autoActiveBaseBean.getAllautobase();

        if (allActives != null && allActives.size() > 0) {
            for (int i = 0; i < allActives.size(); i++) {
                AutoActiveBase activeBase = allActives.get(i);
                if (activeBase != null && activeBase.getId() == this.activeBase.getId()) {

                }
            }
        }

        if (icon == null) {
            icon = CutButtonImage.getImage("inkImg/old/background/S342.png", 465, 53);
        }
        g.drawImage(icon.getImage(), 1, 1, 465, 53, null);

        Font TEXT_FONT60 = new Font("hywb", Font.BOLD, 16);
        Color color = new Color(147, 39, 30);
        Color color2 = new Color(2, 240, 30);
        g.setFont(TEXT_FONT60);
        g.setColor(color);
        g.drawString(activeBase.getaName() + "（已完成", 30, 30);
        g.setColor(Color.RED);
        if (activeBase.getaName().length()==2) {
            g.drawString(String.valueOf(activeBase.getComNum()), 10 + 21 * (activeBase.getaName().length() + 4), 30);
        }else if (activeBase.getaName().length()%3==0) {
            g.drawString(String.valueOf(activeBase.getComNum()), 8 + 21 * (activeBase.getaName().length() + 4), 30);
        }else{
            g.drawString(String.valueOf(activeBase.getComNum()), 5 + 21 * (activeBase.getaName().length() + 4), 30);
        }
        g.setColor(color);
        if (String.valueOf(activeBase.getComNum()).length()%2==0) {
            g.drawString("次）", 10 + 21 * (activeBase.getaName().length() + 4 + (int) Math.ceil(String.valueOf(activeBase.getComNum()).length() / 2f)), 30);
        }else if (String.valueOf(activeBase.getComNum()).length()%3==0) {
            g.drawString("次）", 21 * (activeBase.getaName().length() + 4 + (int) Math.ceil(String.valueOf(activeBase.getComNum()).length() / 2f)), 30);
        }else{
            g.drawString("次）", 21 * (activeBase.getaName().length() + 4 + (int) Math.ceil(String.valueOf(activeBase.getComNum()).length() / 2f)), 30);
        }
    }




    public JTextField getNumField() {
        if (numField == null) {
            numField = new JTextField();
            numField.setText("0");
            numField.setFont(UIUtils.TEXT_FONT1B);
            numField.setBackground(UIUtils.Color_BACK);
            numField.setBorder(BorderFactory.createEmptyBorder());
            numField.setForeground(Color.white);
            numField.setCaretColor(Color.white);
            numField.setBounds(392, 14, 60, 22);
            this.add(numField);
        }
        return numField;
    }




    public JCheckBox getJCheckBox() {
        if (checkBox == null) {
            ImageIcon icon = new ImageIcon("inkImg/old/background/B461.png");
            ImageIcon selectIcon = new ImageIcon("inkImg/old/background/B460.png");
            checkBox = new JCheckBox(icon, false);
            checkBox.setBackground(UIUtils.Color_BACK);
            checkBox.setSelectedIcon(selectIcon);
            checkBox.setBounds(5, 15, 20, 20);
//                checkBox.addChangeListener(new ChangeListener(){
//                    @Override
//                    public void stateChanged(ChangeEvent e) {
//                       JCheckBox target = (JCheckBox)e.getSource();
//                       System.out.print("sss");
//                    }
//                });
            this.add(checkBox);
        }
        return checkBox;
    }

    public ImageIcon getIcon() {
        return icon;
    }

    public void setIcon(ImageIcon icon) {
        this.icon = icon;
    }

    public AutoActiveBase getActiveBase() {
        return activeBase;
    }

    public void setActiveBase(AutoActiveBase activeBase) {
        this.activeBase = activeBase;
    }

//    public JLabel getLabName() {
//        if (labName == null) {
//            labName = new JLabel();
//            labName.setBounds(30, 12, 400, 20);
//            Font TEXT_FONT60 = new Font("汉仪小隶书", Font.BOLD, 20);
//            labName.setFont(TEXT_FONT60);
//            Color color = new Color(147, 39, 30);
//            labName.setForeground(color);
//            this.add(labName);
//        }
//        return labName;
//    }
//
//    public void setLabName(JLabel labName) {
//        this.labName = labName;
//    }

}
